import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path';
import * as dotenv from 'dotenv';

// Load environment variables from .env file using absolute path
dotenv.config({ path: path.resolve(__dirname, '.env.development') });

const env = process.env;

console.log('Environment variables:');
console.log('VITE_APP_BASE_API:', env.VITE_APP_BASE_API);
console.log('VITE_SERVER_HOST:', env.VITE_SERVER_HOST);
console.log('VITE_SERVER_PORT:', env.VITE_SERVER_PORT);

export default defineConfig(() => {
  return {
    plugins: [vue()],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/styles/variables.scss" as *;`,
        },
      },
    },
    server: {
      port: 5173, // 设置前端服务的端口号
      proxy: {
        [env.VITE_APP_BASE_API || '/api']: {
          target: "http://localhost:8080/",
          changeOrigin: true,
          rewrite: (path) => {
            console.log('Original path:', path);
            // Do not modify the original path, return it as is.
            return path;
          },
          logLevel: 'debug',
        },
      },
    },
  };
});